define([], function() {
    'use strict';
    let _zqLevelNow = 0;
    let _zqLevelBefore = 0;
    let codeIndexObj = {};

    let CST = {
        dataField: "num",
        zoomMapping: {},
        serverUrl: "./geoLocation",
    }

    function init(params) {
        $.extend(CST, params);
        getcodeIndex();
    }


    function getdataByParentCode(filterCode) {
        // let dataNow = [];
        // $.ajax({
        //     type: "get", //请求方式
        //     url: `./src/source/data/start/${filterCode}.json`, //文件地址
        //     async: false,
        //     success: function(data) { //成功返回
        //         dataNow = dataNow.concat(data.features);
        //     },
        //     error: function(e) { //失败返回
        //         console.log(e.statusText);
        //     }
        // });
        // return pretreatmentData(dataNow);

        return [
            {name: "测试1", id: filterCode + "001", pid: filterCode},
            {name: "测试2", id: filterCode + "002", pid: filterCode},
            {name: "测试3", id: filterCode + "003", pid: filterCode},
            {name: "测试4", id: filterCode + "004", pid: filterCode},
            {name: "测试5", id: filterCode + "005", pid: filterCode}
        ];
    }

    function getdataByLevel(zoom, extent) {
        let dataNow = {
            datas: [],
            isAnimate: true,
        };
        // 根据级别控制聚合、散开
        Object.keys(CST.zoomMapping).forEach(item => {
            if (zoom >= CST.zoomMapping[item][0] && zoom < CST.zoomMapping[item][1]) {
                _zqLevelBefore = _zqLevelNow;
                _zqLevelNow = parseInt(item);
            }
        })
        
        if (_zqLevelNow === 0) {
            $.ajax({
                type: "get", //请求方式
                url: `./src/source/data/start/200000.json`, //文件地址
                async: false,
                success: function(data) { //成功返回
                    dataNow.datas = dataNow.datas.concat(data.features);
                },
                error: function(e) { //失败返回
                    console.log(e.statusText);
                }
            });
        }else{
            AjaxzqDataAsyncByExtent(_zqLevelNow-1, extent, function (res) {
                res.forEach(item => {
                    if (codeIndexObj[item.code]) {
                        $.ajax({
                            type: "get", //请求方式
                            url: `./src/source/data/start/${item.code}.json`, //文件地址
                            async: false,
                            success: function(data) { //成功返回
                                if (_zqLevelNow === 4) {
                                    data.features[0]["num"] = "5";
                                }
                                dataNow.datas = dataNow.datas.concat(data.features);
                            },
                            error: function(e) { //失败返回
                                console.log(e.statusText);
                            }
                        });
                    }
                })
            });
        }
        if (_zqLevelNow === _zqLevelBefore) {
            dataNow.isAnimate = false;
        }
        dataNow.datas.length && (dataNow.datas = pretreatmentData(dataNow.datas));
        return dataNow;
    }

    function pretreatmentData(dataArr) {
        let dataFieldMaxValue = parseInt(dataArr[0][CST.dataField]);
        let dataFieldMinValue = parseInt(dataArr[0][CST.dataField]);
        dataArr.forEach(element => {
            if (parseInt(element[CST.dataField]) > dataFieldMaxValue) {
                dataFieldMaxValue = parseInt(element[CST.dataField]);
            }
            if (parseInt(element[CST.dataField]) < dataFieldMaxValue) {
                dataFieldMinValue = parseInt(element[CST.dataField]);
            }
        });
        return dataArr.map(item => {
            if (parseInt(item["num"]) > 1) {
                item["type"] = "aggregation";
            }else{
                item["type"] = "scatter";
            }
            item[CST.dataField] = parseInt(item[CST.dataField]);
            item["maxvalue"] = dataFieldMaxValue;
            item["minvalue"] = dataFieldMinValue;
            return item;
        })
    }

    function getcodeIndex() {
        $.ajax({
            type: "get", //请求方式
            url: `./src/source/data/start/codeIndex.json`, //文件地址
            async: true,
            success: function(data) { //成功返回
                codeIndexObj = Object.assign({}, data);
            },
            error: function(e) { //失败返回
                console.log(e.statusText);
            }
        });
    }

    
    function AjaxzqDataAsyncByExtent(level, extent, callback) {
        $.ajax({
            url: CST.serverUrl + "/rest/zq/queryCodesInExtent/2020/" + level + "/" + extent.join("/"),
            type: "GET",
            dataType: "JSON",
            async: false,
            success: function (res) {
                res && callback(res);
            },
            error: function (res) {
                console.error("获取数据失败！");
            },
        });
    }


    return {
        init: init,
        getdataByLevel: getdataByLevel,
        getdataByParentCode: getdataByParentCode
    }
});